import { createSlice } from "@reduxjs/toolkit";
// 定义初始化状态
const initialState = {
  value:1
}
export const  counterSlice = createSlice({
  name:"counter", //id名称
  initialState,
  reducers:{ //定义处理数据的方法,加1,减1,加n
    increment:state=>{state.value+=1},  
    decrement:state=>{state.value-=1},
    addVal:(state,action)=>{state.value+=action.payload}
  }
})

// 全部动作导出
export const {increment,decrement,addVal} = counterSlice.actions;
// 导出默认处理器
export default counterSlice.reducer;

// 异步动作
export const addSync = (num)=>{
  return dispatch=>{
    // axios.get("xxx").then(res=>{})
     setTimeout(()=>{
      dispatch(addVal(num))
     },5000)
  }
}